package icu.twtool.ui.component

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.requiredHeight
import androidx.compose.foundation.layout.requiredWidth
import androidx.compose.foundation.window.WindowDraggableArea
import androidx.compose.material.LocalContentColor
import androidx.compose.material.LocalTextStyle
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.DpSize
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog
import androidx.compose.ui.window.DialogState
import androidx.compose.ui.window.rememberDialogState
import icu.twtool.ui.style.MyColors
import icu.twtool.ui.style.MyShape

/**
 * 确认对话框
 * @author Wen
 * @since 2022-11-02
 */
@Composable
@Suppress("FunctionName")
fun ConfirmDialog(title: String, desc: String = "", state: DialogState = rememberDialogState(size = DpSize(300.dp, 200.dp)), onCloseRequest: () -> Unit, onConfirmRequest: () -> Unit) {
    Dialog(onCloseRequest, state, resizable = false, undecorated = true, transparent = true) {
        WindowDraggableArea {
            Box(Modifier.padding(16.dp)) {
                Surface(
                    color = MyColors.SecondaryBackground,
                    contentColor = MyColors.OnSecondaryBackground,
                    elevation = 10.dp,
                    shape = MyShape.RoundedCornerLarge
                ) {
                    CompositionLocalProvider(LocalContentColor provides MyColors.OnSecondaryBackground) {
                        Column(Modifier.fillMaxWidth().padding(16.dp)) {
                            Text(title, fontSize = MaterialTheme.typography.h5.fontSize)
                            if (desc.isNotEmpty()) {
                                Spacer(Modifier.requiredHeight(16.dp))
                                Text(desc, style = MaterialTheme.typography.caption, color = LocalContentColor.current.copy(0.6f))
                            }
                            Spacer(Modifier.requiredHeight(16.dp))
                            Row(
                                Modifier.fillMaxWidth(),
                                horizontalArrangement = Arrangement.End,
                                verticalAlignment = Alignment.CenterVertically
                            ) {
                                CompositionLocalProvider(LocalTextStyle provides MaterialTheme.typography.h6) {
                                    TertiaryTextButton("取消") {
                                        onCloseRequest()
                                    }
                                    Spacer(Modifier.requiredWidth(16.dp))
                                    TertiaryTextButton("确定") {
                                        onConfirmRequest()
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}